<?php
namespace Home\Controller;
use Think\Controller;
use Home\Biz\AdminBiz;
use Home\Biz\PromoterBiz;
use Home\Biz\CommonBiz;
use Home\Biz\UserBiz;
use Home\Biz\OrderBiz;

/**
 * 页面ajax请求服务处理Controller
 * @author Simon
 *
 */
class AjaxServiceController extends Controller {
	
	/**
     * Ajax服务中转类
     */
	public function center($moudle, $util){

		//安全校验begin
		$request_param_array = CommonBiz::getRequestParams();
		if(isset($request_param_array['promoter_id'])){
			$promoter_id = $request_param_array['promoter_id'];
			
			$login_safecode_cookie = cookie(C('AdminConfig.login_safecode_cookie_name'));
			$safe_code = md5($promoter_id.C('SystemConfig.encrypt_key').date("y-m-d", time()));
			
			if($safe_code != $login_safecode_cookie){
				//安全校验错误
				$this->responseFail(5, '安全校验错误，请重新登录！');
				return;
			}
		}
		//安全校验end
		
		switch(strtolower($moudle)){
			case 'common':
				$this->commonService($util);
				break;
			case 'admin':
				$this->adminService($util);
				break;
			case 'order':
				$this->orderService($util);
				break;
			case 'promoter':
				$this->promoterService($util);
				break;
			default:
				$this->responseFail(5, '无法找到服务模块');
		}
    }
    
    public function commonService($util){
    	switch($util){
    		case 'getAllProvince':
    			$lst_en = CommonBiz::getAllProvince();
    			$this->responseSuccess($lst_en);
    			break;
    		case 'getCityListByProvinceId':
    			$province_id = I('province_id');
    			$lst_en = CommonBiz::getCityListByProvinceId($province_id);
    			$this->responseSuccess($lst_en);
    			break;
    		case 'getDistrictListByCityId':
    			$city_id = I('city_id');
    			$lst_en = CommonBiz::getDistrictByCityId($city_id);
    			$this->responseSuccess($lst_en);
    			break;
    		default:
    			$this->responseFail(5, '无法找到服务方法');
    			break;
    	}
    }
    
    
    /**
     * admin 模块
     * @param string $util 方法名
     */
    public function adminService($util){
    	switch($util){
    		case 'login':
    			//用户登陆
    			
    			//手机号码或者编号
    			$cellphone = I('param.cellphone');
    			$password = I('param.password');
    			 
    			$error_msg = AdminBiz::login($cellphone, $password);
    			 
    			if(empty($error_msg)){
    				$this->responseSuccess ();
				} else {
					$this->responseFail ( 5, $error_msg );
				}
				break;
			case 'employeeLogin' :
				// 用户登陆
				
				// 手机号码或者编号
				$employee_id	= I ('employee_id');
				$employee_name	= I ('employee_name');
				$cellphone		= I ('cellphone');
				$create_user	= 'whaleyFZ AjaxService adminService.employeeLogin';
				
				$error_msg = AdminBiz::employeeLogin ( $employee_id, $employee_name, $cellphone, $create_user);
				
				if (empty ( $error_msg )) {
					$this->responseSuccess();
    				}
    				else{
    					$this->responseFail(5, $error_msg);
    				}
    				break;
    		case 'logout':
    			cookie(C('AdminConfig.login_cookie_name'),
    					null,
    					array('domain'=>C('AdminConfig.login_cookie_domain')));
    			$this->responseSuccess();
    			break;
    		case 'verifyRegister':
    			//校验注册信息
    			$promoter_en = array(
    					'promoter_type'	=> I('promoter_type'),
    					'sex'			=> I('sex'),
    					'promoter_name' => I('real_name'),
    					'real_name'		=> I('real_name'),
    					'password'		=> I('password'),
    					'alipay_id'		=> I('alipay_id'),
    					'alipay_name'		=> I('alipay_name'),
    					'cellphone'		=> I('cellphone'),
    					'id_card_no'	=> I('id_card_no'),
    					'district_id'	=> I('district_id'),
    					'address'		=> I('address'),
    			);
    			$recommend_share_code = I('share_code');
    			$error_msg = AdminBiz::verifyRegister($promoter_en, $recommend_share_code);
   				if(empty($error_msg)){
   					$this->responseSuccess();
   				}
   				else{
   					$this->responseFail(5, $error_msg);
   				}
   				break;
    		case 'register':
    			//用户注册
    			$promoter_en = array(
	    				'promoter_type'	=> I('promoter_type'),
    					'sex'			=> I('sex'),
    					'promoter_name' => I('real_name'),
	    				'real_name'		=> I('real_name'),
	    				'password'		=> I('password'),
	    				'alipay_id'		=> I('alipay_id'),
						'cellphone'		=> I('cellphone'),
						'id_card_no'	=> I('id_card_no'),
						'business_license_no' => I('business_license_no'),
						'district_id'	=> I('district_id'),
						'address'		=> I('address'),
						'register_ip'	=> get_client_ip(),
						'register_date' => date("y-m-d H:i:s", time()),
						'status'		=> 0,
						'create_user'	=> 'whaleyFZ AjaxService adminService.register',
    					'create_date'	=> date("y-m-d H:i:s", time()),
    			);
    			$recommend_share_code = I('share_code');
    			
    			$error_msg = AdminBiz::register($promoter_en, $recommend_share_code);
    			if(empty($error_msg)){
    				$this->responseSuccess();
    			}
    			else{
    				$this->responseFail(5, $error_msg);
    			}
    			break;
    		default:
    			$this->responseFail(5, '无法找到服务方法');
    			break;
    	}
    }


    /**
     * @param $util
     */
    public function orderService($util){
    	switch($util){
            case 'calcCartAmount':
                //计算购物车金额
                $cart_en    =OrderBiz::calcCart(I('lst_sku'));

                $return_en   = array(
                    'goods_qty'     => $cart_en['goods_qty'],
                    'goods_amount'  => $cart_en['goods_amount'],
                    'promotion_amount'  => $cart_en['promotion_amount'],
                    'no_freight_order_amount'   => $cart_en['no_freight_order_amount'],
                    'order_amount'      => $cart_en['order_amount'],
                );

                $this->responseSuccess($return_en);
                break;
    		case 'submitOrder':
    			//业务执行begin
                $address_id = I('address_id');
                $lst_sku    = I('lst_sku');
                $order_id   = OrderBiz::generateOrderId();
                $return_en  = OrderBiz::submitOrder($address_id, $lst_sku, $order_id);
    			//业务执行end
    			 
    			if($return_en['is_success'] == 1){
    				$order_id   = $return_en['msg'];
    				$url = C('UrlConfig.order_success_url').'?order_id='.$order_id;
    				$this->responseSuccess($url);
    			}
    			else{
                    $error_msg  = $return_en['msg'];
    				$this->responseFail(5, $return_en['msg']);
    			}
    			break;
    		case 'addAddress':
    			//业务执行begin
    			$error_msg	= '';
    			$lst_sku	= I('lst_sku');
    			
    			try{
    				$address_id= UserBiz::generateAddressId();
    				$user_id	= I('user_id');
    				$error_msg = OrderBiz::addAddress(I('param.'), $user_id, $address_id);
    			}
    			catch(Exception $e){
    				$error_msg = $e->getMessage();
    			}
    			//业务执行end
    			
    			if(empty($error_msg)){
    				$url = C('UrlConfig.checkout_url').'?lst_sku='.$lst_sku.'&address_id='.$address_id;
    				$this->responseSuccess($url);
    			}
    			else{
    				$this->responseFail(5, $error_msg);
    			}
    			break;
    		case 'updateAddress':
    			//业务执行begin
    			$error_msg	= '';
    			$address_id = I('address_id');
    			$lst_sku	= I('lst_sku');
    			
    			try{
    				$error_msg = OrderBiz::updateAddress($_POST, $address_id);
    			}
    			catch(Exception $e){
    				$error_msg = $e->getMessage();
    			}
    			//业务执行end
    			
    			if(empty($error_msg)){
    				
    				$url = C('UrlConfig.checkout_url').'?lst_sku='.$lst_sku.'&address_id='.$address_id;
    				$this->responseSuccess($url);
    			}
    			else{
    				$this->responseFail(5, $error_msg);
    			}
    			break;
    		case 'getOrderList':
    			$error_msg	= '';
    			$promoter_id= I('promoter_id');
    			$type		= I('type');
    			$index		= I('index');
    			$size		= I('size');
    			
    			$result	= OrderBiz::getOrderList($promoter_id, $type, $index, $size);
    	
    			if(gettype($result) == 'string'){
    				//有错误返回
    				$error_msg = $result;
    				$this->responseFail(5, $error_msg);
    			}
    			else{
    				$lst_order_en = $result;

    				$this->responseSuccess($lst_order_en);
    			}
    			
    			break;
    		case 'searchOrder':
    			$keyword = I('keyword');
    			$lst_order_en = OrderBiz::searchOrder($keyword);
    			
    			$this->responseSuccess($lst_order_en);
    			break;
    		case 'cancelOrder':
    			$order_id = I('order_id');
    			$error_msg = OrderBiz::cancelOrder($order_id, 'whaleyFZ AjaxService orderService.cancelOrder');
    			if(empty($error_msg)){
    				$this->responseSuccess();
    			}
    			else{
    				$this->responseFail(5, $error_msg);
    			}
    			break;
    		default:
    			$this->responseFail(5, '无法找到服务方法');
    			break;
    	}
    }
    
    
    
    /**
     * promoter 模块
     * @param string $util 方法名
     */
    public function promoterService($util){
    	switch($util){
    		case 'updateInfo':
    			$promoter_en = array(
    					'promoter_id'	=> I('promoter_id'),
    					'sex'			=> I('sex'),
    					'promoter_name' => I('real_name'),
	    				'real_name'		=> I('real_name'),
	    				'alipay_id'		=> I('alipay_id'),
	    				'alipay_name'	=> I('alipay_name'),
						'cellphone'		=> I('cellphone'),
						'id_card_no'	=> I('id_card_no'),
						'business_license_no' => I('business_license_no'),
						'district_id'	=> I('district_id'),
						'address'		=> I('address'),
						'update_user'	=> 'whaleyFZ AjaxService promoterService.updateInfo',
    					'update_date'	=> date("y-m-d H:i:s", time()),
    			);
    			 
    			$error_msg = PromoterBiz::updateInfo($promoter_en);
    			 
    			if(empty($error_msg)){
    				$this->responseSuccess();
    			}
    			else{
    				$this->responseFail(5, $error_msg);
    			}
    			break;
    		case 'updatePwd':
    			//更新密码
    			$promoter_id= I('promoter_id');
    			$old_pwd	= I('old_pwd');
    			$new_pwd	= I('new_pwd');
    			$new_pwd2	= I('new_pwd2');
    			
    			$error_msg = PromoterBiz::updatePwd($promoter_id, $old_pwd, $new_pwd, $new_pwd2);
    			
    			if(empty($error_msg)){
    				$this->responseSuccess();
    			}
    			else{
    				$this->responseFail(5, $error_msg);
    			}
    			break;
    		case 'reRegister':
    			$promoter_id= I('promoter_id');
    			$error_msg = PromoterBiz::reRegister($promoter_id);
    			if(empty($error_msg)){
    				$this->responseSuccess();
    			}
    			else{
    				$this->responseFail(5, $error_msg);
    			}
    			break;
    		case 'getAmountList':
    			$promoter_id= I('promoter_id');
    			$page_index = I('page_index');
    			$page_size	= C('PromoterConfig.withdraw_list_size');
    			$lst_amount_en = PromoterBiz::getAmountList($promoter_id, $page_index, $page_size);
    			$this->responseSuccess($lst_amount_en);
    			break;
    		case 'getWithdrawList':
    			$promoter_id= I('promoter_id');
    			$page_index = I('page_index');
    			$page_size	= C('PromoterConfig.amount_list_size');
    			$lst_amount_en = PromoterBiz::getWithdrawList($promoter_id, $page_index, $page_size);
    			$this->responseSuccess($lst_amount_en);
    			break;
    		case 'withdraw':
    			//申请提现
    			$error_msg = PromoterBiz::withdraw(I('promoter_id'), I('amount'), 'whaleyFZ AjaxService promoterService.withdraw');
    			
    			if(empty($error_msg)){
    				$this->responseSuccess();
    			}
    			else{
    				$this->responseFail(5, $error_msg);
    			}
    			break;
    		
    		default:
    			$this->responseFail(5, '无法找到服务方法');
    			break;
    	}
    }
    
    
    public function responseSuccess($result){
    	//0，正确；1，参数错误；2,安全码校验失败;3，无法反序列化；4,服务器错误;5，业务逻辑错误
    	$return_json = array(
    			'IsSuccess' => 1,
    			'ErrorMsg' => '',
    			'ErrorCode' => 0,
    			'Result' => $result,
    	);
    	$this->ajaxReturn($return_json);
    }
    
    public function responseFail($error_code, $error_msg){
    	//0，正确；1，参数错误；2,安全码校验失败;3，无法反序列化；4,服务器错误;5，业务逻辑错误
    	$return_json = array(
    			'IsSuccess' => 0,
    			'ErrorMsg' => $error_msg,
    			'ErrorCode' => $error_code,
    			'Result' => null,
    	);
    	$this->ajaxReturn($return_json);
    }
}